<template>
  <div class="login-container">
    <!-- 头部 -->
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >

      <div class="title-container">
        <h3 class="title">
          <img
            src="../../assets/logo.595745bd.png"
            alt=""
          >
        </h3>
      </div>
      <!-- 账号 -->
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
        />
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <span
          class="show-pwd"
          @click="showPwd"
        >
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <!-- 验证码 -->
      <el-form-item prop="yanzhengma">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="yanzhengma"
          v-model="loginForm.yanzhengma"
          type="text"
          placeholder="请输入验证码"
          name="yanzhengma"
        />
        <span class="show-pwd">
          <div
            class="img_yzm"
            @click="getImgYzm()"
          >
            <img
              :src="img_yzm"
              alt=""
            >
          </div>
        </span>
      </el-form-item>

      <el-button
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click.native="loginFn"
      >登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { getImgYzmAPI, toLoginAPI, profilephotoAPI } from '@/api'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码不能小于3位数字'))
      } else {
        callback()
      }
    }
    const validateYzm = (rule, value, callback) => {
      if (value.length !== 4) {
        callback(new Error('请输入四位数的验证码'))
      } else {
        callback()
      }
    }
    return {
      img_yzm: '',
      clientToken: 'abc',
      loginForm: {
        username: '',
        password: '',
        yanzhengma: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        yanzhengma: [{ required: true, trigger: 'blur', validator: validateYzm }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  created() {
    this.getImgYzm()
  },
  methods: {
    showPwd() {
      this.passwordType === 'password' ? this.passwordType = '' : this.passwordType = 'password'
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录
    loginFn() {
      this.$refs.loginForm.validate(async(vail) => {
        if (vail) {
          try {
            // 验证登录
            const res = await toLoginAPI({
              loginName: this.loginForm.username,
              password: this.loginForm.password,
              code: this.loginForm.yanzhengma,
              clientToken: this.clientToken,
              loginType: 0
            })
            console.log(res)
            this.$store.commit('user/GET_TOKEN', res.data.token)
            // 获取用户信息
            const result = await profilephotoAPI(res.data.userId)
            console.log(result)
            this.$store.commit('user/GET_USERINFO', result.data)
            this.$router.push('/')
          } catch (error) {
            console.log(error)
          }
        }
      })
    },
    // 请求验证码接口
    async getImgYzm() {
      try {
        this.img_yzm = ''
        const res = await getImgYzmAPI(this.clientToken)
        this.img_yzm = res.request.responseURL
      } catch (e) {
        console.log(e)
      }
    }

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */

.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #999;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.el-input__inner {
  background-color: #fff;
}
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url(../../assets/404_images/login.jpg);
  .login-form {
    position: absolute;
    margin: auto;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 10px;
    padding: 0 36px;
    width: 520px;
    height: 388px;
    background-color: #fff;
    .el-form-item {
      border: 1px solid #e2e2e2;
      background-color: #fff;
      color: #2d3a4b;
      .img_yzm {
        position: absolute;
        top: -6px;
        right: -10px;
        height: 50px;
        width: 130px;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }

  .tips {
    font-size: 14px;
    color: #e2e2e2;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    height: 76px;
    .title {
      position: relative;
      top: -50px;
      width: 96px;
      height: 96px;
      margin: 0px auto;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
